<?php
namespace app\common\lib\sms;

use Qcloud\Sms\SmsSingleSender;
use think\facade\Log;


class TCloudSms implements SmsBase
{

    const APPID = ''; // SDK AppID 以1400开头

    const APPKEY = "";

    const TEMPLATE_ID = '';

    const SMS_SIGN = "腾讯云";

    public static function sendCode(string $phone, int $code): bool
    {

        if (empty($phone) || empty($code)) {
            return false;
        }

        try {
            $ssender = new SmsSingleSender(self::APPID, self::APPKEY);
            $params = [$code];
            $result = $ssender->sendWithParam("86", $phone, self::TEMPLATE_ID,
                $params, self::SMS_SIGN, "", "");
            $rsp = json_decode($result, true);
            Log::info($rsp);
        } catch(\Exception $e) {
            Log::error($e->getMessage());
            return false;
        }
        if (isset($rsp['result']) && $rsp['result'] == 0) {
            return true;
        }
        return false;
    }
}